package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.input.QueryVehicleBlacklistBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleBlacklistBO;
import com.xhwl.logistics.dao.mapper.IVehicleBlacklistMapper;
import com.xhwl.logistics.entity.VehicleBlacklist;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

//@Service
public class VehicleBlacklistServiceImpl extends ServiceImpl<IVehicleBlacklistMapper, VehicleBlacklist> {

    public Wrapper<VehicleBlacklist> conditionForQueryBlacklists(Integer projectId) {
        return conditionForQueryBlacklists(projectId, null, null, null);
    }

    public Wrapper<VehicleBlacklist> conditionForQueryBlacklists(Integer projectId, String plateNumber) {
        return conditionSecondForQueryBlacklists(projectId, plateNumber);
    }

    public Wrapper<VehicleBlacklist> conditionForQueryBlacklists(QueryVehicleBlacklistBO queryVehicleBlacklistBO) {
        return conditionForQueryBlacklists(queryVehicleBlacklistBO.getProjectId(), queryVehicleBlacklistBO.getParkingLotName(), queryVehicleBlacklistBO.getPlateNumber(), queryVehicleBlacklistBO.getPlateOwnerName());
    }

    private Wrapper<VehicleBlacklist> conditionForQueryBlacklists(Integer projectId, String parkingLotName, String plateNumber, String plateOwnerName) {
        LambdaQueryWrapper<VehicleBlacklist> vehicleBlacklistLambdaQueryWrapper = Wrappers.lambdaQuery();
        //LambdaQueryWrapper<VehicleBlacklist> vehicleBlacklistLambdaQueryWrapper = new LambdaQueryWrapper<>(VehicleBlacklist.class)
        vehicleBlacklistLambdaQueryWrapper.eq(VehicleBlacklist::getProjectId, projectId)
                .eq(StringUtils.isNotEmpty(parkingLotName), VehicleBlacklist::getParkingLotName, parkingLotName);
        if(StringUtils.isNotEmpty(plateNumber)){
            vehicleBlacklistLambdaQueryWrapper.and(Wrapper->
                    Wrapper.like(VehicleBlacklist::getPlateNumber, plateNumber)
                            .or()
                            .like(VehicleBlacklist::getPlateOwnerName, plateNumber)
            );
        }
        return vehicleBlacklistLambdaQueryWrapper;
//                .like(StringUtils.isNotEmpty(plateNumber), VehicleBlacklist::getPlateNumber, plateNumber)
//                .like(StringUtils.isNotEmpty(plateOwnerName), VehicleBlacklist::getPlateOwnerName, plateOwnerName);
    }

    private Wrapper<VehicleBlacklist> conditionSecondForQueryBlacklists(Integer projectId, String plateNumber) {
        LambdaQueryWrapper<VehicleBlacklist> queryWrapper = Wrappers.lambdaQuery();
        return queryWrapper
                .eq(VehicleBlacklist::getProjectId, projectId)
                .eq(VehicleBlacklist::getPlateNumber, plateNumber);
    }

    private Wrapper<VehicleBlacklist> conditionForDeleteBlacklist(DeleteVehicleBlacklistBO deleteVehicleBlacklistBO) {
        LambdaUpdateWrapper<VehicleBlacklist> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .eq(VehicleBlacklist::getId, deleteVehicleBlacklistBO.getId())
                .set(VehicleBlacklist::getDeleted, 1)
                .set(VehicleBlacklist::getDeleterAccountId, deleteVehicleBlacklistBO.getDeleterAccountId())
                .set(VehicleBlacklist::getDeleterAccountName, deleteVehicleBlacklistBO.getDeleterAccountName())
                .set(VehicleBlacklist::getUpdatedAt, new Date());
    }
    private Wrapper<VehicleBlacklist> conditionForDeleteBlacklists(List<Integer> idList) {
        LambdaUpdateWrapper<VehicleBlacklist> updateWrapper = Wrappers.lambdaUpdate();
        return updateWrapper
                .in(VehicleBlacklist::getId, idList)
                .set(VehicleBlacklist::getDeleted, 1)
                .set(VehicleBlacklist::getDeleterAccountName, "xxl-job")
                .set(VehicleBlacklist::getUpdatedAt, new Date());
    }

    public void deleteBlacklist(DeleteVehicleBlacklistBO deleteVehicleBlacklistBO) {
        update(conditionForDeleteBlacklist(deleteVehicleBlacklistBO));
    }

    public void deleteBlacklists(List<Integer> idList) {
        update(conditionForDeleteBlacklists(idList));
    }

    public Optional<VehicleBlacklist> queryVehicleBlacklist(Integer projectId, String plateNumber) {
        VehicleBlacklist vehicleBlacklist;
        try {
            vehicleBlacklist = getOne(conditionForQueryBlacklists(projectId, plateNumber));
        } catch (Exception e) {
            throw new BusinessException("根据项目id和车牌号码，黑名单查询结果不唯一，有多个结果", e);
        }
        return Optional.ofNullable(vehicleBlacklist);
    }

}
